﻿using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Kopytko
{
    /// <summary>
    /// Interaction logic for OverlayMessageBox.xaml
    /// </summary>
    public partial class OverlayMessageBox : UserControl
    {
        public OverlayMessageBox()
        {
            InitializeComponent();

        }

        private bool _hideRequest = false;
        private bool _result = false;
        private UIElement _parent;

        public void SetParent(UIElement parent)
        {
            _parent = parent;
        }

        #region Message

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.
        // This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register(
                "Message", typeof(string), typeof(OverlayMessageBox),
                new UIPropertyMetadata(string.Empty));

        #endregion

        public void ShowDialog(MessageBoxBase box)
        {
            Visibility = Visibility.Visible;

            cc_main.Content = box;
            box.BeforeClose += new System.EventHandler(box_BeforeClose);

            //_parent.IsEnabled = false;

            _hideRequest = false;
            while (!_hideRequest)
            {
                // HACK: Stop the thread if the application is about to close
                if (this.Dispatcher.HasShutdownStarted ||
                    this.Dispatcher.HasShutdownFinished)
                {
                    break;
                }

                // HACK: Simulate "DoEvents"
                this.Dispatcher.Invoke(
                    DispatcherPriority.Background,
                    new ThreadStart(delegate { }));
                Thread.Sleep(20);
            }

//            return _result;
        }

        void box_BeforeClose(object sender, System.EventArgs e)
        {
            HideDialog();
        }

        private void HideDialog()
        {
            _hideRequest = true;
            Visibility = Visibility.Hidden;
            //_parent.IsEnabled = true;
        }

        
    }
}
